package com.zisu.rabbitproducer.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zisu.rabbitproducer.Entity.User;
import com.zisu.rabbitproducer.component.RabbitSender;
import com.zisu.rabbitproducer.component.RabbitSenderEQ2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2020/9/30 11:03
 */
@RestController
public class TestController {
    @Autowired
    RabbitSender rabbitSender;

    @Autowired
    RabbitSenderEQ2 rabbitSenderEQ2;

    //For  EAP SERVER1
    @GetMapping("/putMessageDirect")
    public String putMessageDirect() throws Exception {
        for(int i=0;i<10;i++){
            Map<String,Object> objectMap = new HashMap<>();
            objectMap.put("attr1",String.valueOf(i));
            objectMap.put("attr2","ABCDE");
            //序列化
            User cxcx = new User("cxcx", 11,"12323");
            ObjectMapper objectMapper = new ObjectMapper();
            byte[] bytes = objectMapper.writeValueAsBytes(cxcx);
            rabbitSender.sendDirect(bytes,objectMap,"eq1_response");
            Thread.sleep(30);
        }
        return null;
    }

    //For  EAP SERVER2
    @GetMapping("/putMessageDirectForEQ2")
    public String putMessageDirectForEQ2() throws Exception {
        for(int i=0;i<3;i++){
            Map<String,Object> objectMap = new HashMap<>();
            objectMap.put("attr1",String.valueOf(i));
            objectMap.put("attr2","ABCDE");
            //序列化
            User cxcx = new User("cxcx", 11,"12323");
            ObjectMapper objectMapper = new ObjectMapper();
            byte[] bytes = objectMapper.writeValueAsBytes(cxcx);

            rabbitSenderEQ2.sendDirect(bytes,objectMap,"eq5_request");
            Thread.sleep(30);
        }
        return null;
    }

    @GetMapping("/putMessageDirect2")
    public String putMessageDirect2() throws Exception {
        for(int i=0;i<10;i++){
            Map<String,Object> objectMap = new HashMap<>();
            objectMap.put("attr1",String.valueOf(i));
            objectMap.put("attr2","ABCDE");
            //序列化
            User cxcx = new User("cxcx", 11,"12323");
            ObjectMapper objectMapper = new ObjectMapper();
            byte[] bytes = objectMapper.writeValueAsBytes(cxcx);
            rabbitSenderEQ2.sendDirect(bytes,objectMap,"eq5_request");
            Thread.sleep(30);
        }
        return null;
    }

    @GetMapping("/putMessageTopic")
    public String putMessageTopic() throws Exception {
        for(int i=0;i<10;i++){
            Map<String,Object> objectMap = new HashMap<>();
            objectMap.put("attr1",String.valueOf(i));
            objectMap.put("attr2","ABCDE");
            //序列化
            User cxcx = new User("cxcx", 11,"1232");
            ObjectMapper objectMapper = new ObjectMapper();
            byte[] bytes = objectMapper.writeValueAsBytes(cxcx);
            rabbitSender.sendTopic(bytes,objectMap,"RC.EQ1");
            Thread.sleep(30);
        }
        return null;
    }

    @GetMapping("/putMessageTopic2")
    public String putMessageTopic2() throws Exception {
        for(int i=0;i<10;i++){
            Map<String,Object> objectMap = new HashMap<>();
            objectMap.put("attr1",String.valueOf(i));
            objectMap.put("attr2","ABCDE");
            //序列化
            User cxcx = new User("cxcx", 11,"1232");
            ObjectMapper objectMapper = new ObjectMapper();
            byte[] bytes = objectMapper.writeValueAsBytes(cxcx);
            rabbitSender.sendTopic(bytes,objectMap,"ACK.EQ1");
            Thread.sleep(30);
        }
        return null;
    }


    public static void main(String[] args) {
        String s = "KEY1@VALUE1;KEY1@VALU E1;";
        String[] split = s.split(";");
        for(String i:split){
            String[] split1 = i.split("@");
            System.out.println(split1[0]+","+split1[1]);
        }
    }


}
